//
//  YFLastView.m
//  Novel
//
//  Created by 孙云飞 on 2017/11/8.
//  Copyright © 2017年 th. All rights reserved.
//

#import "YFLastView.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
@interface YFLastView()<GDTNativeExpressAdDelegete>

// 用于请求原生模板广告，注意：不要在广告打开期间释放！
@property (nonatomic, retain)   GDTNativeExpressAd *nativeExpressAd;

// 存储返回的GDTNativeExpressAdView
@property (nonatomic, retain)       NSArray *expressAdViews;

// 当前展示模板广告的View（开发者可以自己设置）
@property (strong, nonatomic) UIView *adView;

@end
@implementation YFLastView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        //提示语
        UILabel *tinyLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 2 * kReadSpaceX - 130, 0,130 , 24)];
        tinyLabel.text = @"广告时间，休息一下";
        tinyLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
        tinyLabel.layer.cornerRadius = 12;
        tinyLabel.layer.masksToBounds = YES;
        tinyLabel.font = FONT_SIZE(12);
        tinyLabel.textAlignment = NSTextAlignmentCenter;
        tinyLabel.textColor  = kNormalColor;
        [self addSubview:tinyLabel];
        
        self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithAppkey:@"1106505410" placementId:@"2010223712862664" adSize:CGSizeMake(300, 200)];
        self.nativeExpressAd.delegate = self;
        
        // 拉取5条广告
        [self.nativeExpressAd loadAd:5];
        
        //800X1200
        CGFloat width = kScreenWidth - 4 * kReadSpaceX;
        CGFloat height = 1200 * (width / 800);
        _adView = [[UIView alloc]initWithFrame:CGRectMake(kReadSpaceX, (kScreenHeight - height) / 2 - kReadSpaceY + 5, width, height)];
        _adView.backgroundColor = [UIColor clearColor];
        [self addSubview:_adView];
    }
    return self;
}


/**
 * 拉取广告成功的回调
 */
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd *)nativeExpressAd views:(NSArray<__kindof GDTNativeExpressAdView *> *)views
{
    [self.expressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)obj;
        [adView removeFromSuperview];
    }];
    
    self.expressAdViews = [NSArray arrayWithArray:views];
    if (self.expressAdViews.count) {
        
        // 取一个GDTNativeExpressAdView
        GDTNativeExpressAdView *expressView =  [self.expressAdViews objectAtIndex:0];
        // 设置frame，开发者自己设置
        expressView.frame = CGRectMake(0, 0, CGRectGetWidth(_adView.frame), CGRectGetHeight(_adView.frame));
        expressView.controller = self.rootVC;
        [expressView render];
        //添加View的时机，开发者控制
        [_adView addSubview:expressView];
        
        
    }
    
}

/**
 * 拉取广告失败的回调
 */
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error {
    
}
/**
 * 拉取广告失败的回调
 */
- (void)nativeExpressAdRenderFail:(GDTNativeExpressAdView *)nativeExpressAdView
{
    NSLog(@"%s",__FUNCTION__);
}

- (void)nativeExpressAdViewRenderSuccess:(GDTNativeExpressAdView *)nativeExpressAdView
{
    NSLog(@"%s",__FUNCTION__);
}

- (void)nativeExpressAdViewClicked:(GDTNativeExpressAdView *)nativeExpressAdView
{
    NSLog(@"%s",__FUNCTION__);
}
@end
